package com.saodisheng.controller;

import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.saodisheng.model.NewsFeed;
import com.saodisheng.model.Posts;
import com.saodisheng.service.AdminPostService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class AdminPostController extends BaseController {
	@Autowired
	private AdminPostService adminPostService;

	private NewsFeed feedDao = new NewsFeed().dao();
	public void article() {
		setAttr("postPage", adminPostService.getPostByType(Posts.ARTICLE, getParaToInt("p", 1)));
		render("post.html");
	}
	
	public void video() {
		setAttr("postPage", adminPostService.getPostByType(Posts.VIDEO, getParaToInt("p", 1)));
		render("post.html");
	}
	
	public void gallery() {
		setAttr("postPage", adminPostService.getPostByType(Posts.IMAGE, getParaToInt("p", 1)));
		render("post.html");
	}
	
	public void question() {
		setAttr("postPage", adminPostService.getPostByType(Posts.QUESTION, getParaToInt("p", 1)));
		render("post.html");
	}
	
	public void delete() {
		int postId = getParaToInt("postId");
		Ret ret = adminPostService.deletePost(postId);
		
		List<NewsFeed> newsFeeds = feedDao.find("select id from news_feed where refId=?", postId);
		
		for (int i = 0; i < newsFeeds.size(); i++)
		{
			boolean r2 = Db.deleteById("news_feed", newsFeeds.get(i).getId());
			
			if (!r2)
			{
				renderJson(Ret.fail("msg", "删除news_feed失败"));
				return;
			}
		}
		renderJson(ret);
	}
	
	public void lock() {
		Ret ret = adminPostService.lockPost(getParaToInt("postId"));
		renderJson(ret);
	}
	
	public void unlock() {
		Ret ret = adminPostService.unlockPost(getParaToInt("postId"));
		renderJson(ret);
	}
}
